package be.djdb.aplugin;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import be.djdb.UnConstruct;
import be.djdb.aplugin.plugin.MyPlug;
import be.djdb.aplugin.prog.Plugble;
/**
* @author Lieven Roegiers
* @copyright 2011
* @from JAVA_mylibs
*/
@UnConstruct
public class MyIsplugblescan {
	List<Class> classes1 ;
	List<Class> plugble ;
	/**
	 * 
	 * @param directory
	 * @param packageName
	 * @return
	 * @throws ClassNotFoundException
	 */
	public List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException{
	    List<Class> classes = new ArrayList<Class>();
	    if (!directory.exists()){
	        return classes;
	    }
	    File[] files = directory.listFiles();
	    for (File file : files){
	        if (file.isDirectory()){
	            classes.addAll(findClasses(file, packageName + "." + file.getName()));
	        }else if (isClassfile(file.getName())) {
            	Class theclass = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
            		Annotation[] annotations = theclass.getAnnotations();
            		for(Annotation annotation : annotations){
            		    if(annotation instanceof Plugble){//check can i use a plugin for this class
            		    	Plugble myAnnotation = (Plugble) annotation;
            		    	System.out.println("classname: " +myAnnotation.mustimplement());
            		    	Class myinterface = Class.forName(myAnnotation.mustimplement());
            		    	if(theclass.isInterface()){
            		    		
            		    	}; 
            		    	System.out.println("classname: " +theclass.getName());
            		        System.out.println("value: " + myAnnotation.programname());
            		    }
            		}
            		try {
						//System.out.println("plugin name:"+ annotation.annotationType().getMethod("pluginname").toString());
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
        			classes.add(theclass);
        		}
	    }
	    return classes;
	}
	/**
	 * 
	 * @param name
	 * @return
	 */
	public static boolean isClassfile(String name){
		return name.endsWith(".class");//name.startsWith (this.packageName)
	}
	/**
	 * 
	 * @param aclass
	 * @return
	 */
	private static boolean isplugin(Class aclass){
		return aclass.isAnnotationPresent(MyPlug.class);
	}
	/**
	 * 
	 * @param aclass
	 * @return
	 */
	@UnConstruct
	private String getpluginname(Class aclass){
		return aclass.getAnnotation(MyPlug.class).toString();//TODO not ready
	}
	private void print(Class aclass){
		System.out.println(" |-class:"+ aclass.getName());
	      for (Method m : aclass.getMethods()) {//forName(args[0])
            	System.out.println("|-->Methode"+ m.getName());  
		  }
		     
	}
	private void methodescan(Class aclass){
	      int passed = 0, failed = 0;
	      for (Method m : aclass.getMethods()) {//Class.forName(args[0])
	         if (m.isAnnotationPresent(Plugble.class)) {
	            try {
	               m.invoke(null);
	               passed++;
	            } catch (Throwable ex) {
	               System.out.printf("Test %s failed: %s %n", m, ex.getCause());
	               failed++;
	            }
	         }
	      }
	}
}
